// import {App} from "./app";
// import {GameStar} from "./GameStar";
// import {Ship} from "./Ship";
// import {BulkLoader} from './utils/BulkLoader';
// import {ImageLoader} from './utils/ImageLoader';
///<reference path='./Ship.ts'/>
///<reference path='./app.ts'/>
///<reference path='./GameStar.ts'/>
///<reference path='./utils/BulkLoader.ts'/>
///<reference path='./utils/ImageLoader.ts'/>
class Stars extends lg.Container {

    //     { id: "star", src: "bin/assets/images/star.png" },
    // { id: "astronaut", src: "bin/assets/images/astronaut" + this._resSuffix + ".png" },
    // { id: "celestial-body1", src: "bin/assets/images/celestial-body1" + this._resSuffix + ".png" },
    // { id: "celestial-body2", src: "bin/assets/images/celestial-body2" + this._resSuffix + ".png" },
    // { id: "meteorolite1", src: "bin/assets/images/meteorolite1" + this._resSuffix + ".png" },
    // { id: "meteorolite2", src: "bin/assets/images/meteorolite2" + this._resSuffix + ".png" },
    // { id: "meteorolite3", src: "bin/assets/images/meteorolite3" + this._resSuffix + ".png" },
    // { id: "shooting-star", src: "bin/assets/images/shooting-star" + this._resSuffix + ".png" },
    // { id: "satellite", src: "bin/assets/images/satellite" + this._resSuffix + ".png" }

    private _star:lg.Bitmap;
    private _stars:Array<lg.Bitmap>;
    private _canvas:HTMLCanvasElement;
    private _self:Stars;
    private _ship:Ship;
    constructor(ship:Ship) {
        super();
        this.addEventListener("added",this.addedHandler);
        this._ship=ship;
    }
    private addedHandler(e)
    {
        e.target.removeEventListener("added",e.target.addedHandler);       
        e.target.initView();
    }
    private initView() {
        this._stars=new Array<lg.Bitmap>();
        this._canvas=<HTMLCanvasElement> this.stage.canvas;
        for(var i=0;i<100;i++)
        {
            var star:lg.Bitmap=new lg.Bitmap(App.loadManager.getImage("star"));
            this.addChild(star);
            this._stars.push(star);
            star.addEventListener("tick",(e)=>this.tickHandler(<lg.TickerEvent>e));
            star.x=Math.random()* this._canvas.width;
            star.y=Math.random()* this._canvas.height;
            star.scaleX=star.scaleY=Math.random();
            star.name="s"+i;
            // console.log(this._canvas.width,this._canvas.height);
        }
       this._star=new lg.Bitmap(App.loadManager.getImage("star"));
       this.addChild(this._star);
    }
    private tickHandler(e:lg.TickerEvent)
    {
        if(!this._ship.isBgMove) return;
        // console.log(GameStar.moveY);
        TweenLite.to(this,1,{y:this.y+GameStar.moveY/10});
        var s:lg.Bitmap=<lg.Bitmap>e.target;
      
        if(s.y>this._canvas.height-this.y) 
        {
            // console.log(s.name)
            this.resetStar(s);
        }
    }
    private resetStar(s:lg.Bitmap)
    {
        var self=this;
        var canvasWidth=this._canvas.width;
        setTimeout(function() {
        //    console.log(s.name)
           s.y=-self.y-s.getBounds().height-100;
          s.x=Math.random()* (canvasWidth+20)-s.getBounds().width;
       },Math.random()*300);
        
    }
}